package com.zf.museum.service.impl;

import com.zf.museum.bean.GetResult;
import com.zf.museum.bean.Result;
import com.zf.museum.model.Message;
import com.zf.museum.repository.MessageRepository;
import com.zf.museum.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService{

    @Autowired
    private MessageRepository messageRepository;

    @Override
    public Result addMessage(Message message) {

        try {
            if (messageRepository.getMessageByNumber(message.getNumber()) == null) {
                messageRepository.save(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return GetResult.fail();
        }
        return GetResult.success();
    }

    @Override
    public Result updateMessages(ArrayList<Message> messages) {

        try {
            messageRepository.saveAll(messages);
        } catch (Exception e) {
            e.printStackTrace();
            return GetResult.fail();
        }
        return GetResult.success();
    }

    @Override
    public Result deleteById(Long id) {

        try {
            messageRepository.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return GetResult.fail();
        }

        return GetResult.success();
    }

    @Override
    public List<Message> findAll() {
        return messageRepository.findAll();
    }
}
